﻿using System;
using System.Collections;
using System.Collections.Generic;
using Extensions;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.Mars.Got;

public class RpgFlyTextBuff : MonoBehaviour
{
    public TextMeshPro tmp;
    public RectTransform tmpRect;
    public RectTransform arrowRect;
    public ContentSizeFitter fitter;
    private float fontSize = -1;
    public void SetText(string content,int maxWidth)
    {
        if (fontSize == -1)
        {
            fontSize = tmp.fontSize;
        }
        fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
        tmp.enableAutoSizing = false;
        tmp.text = content;
        tmp.fontSize = fontSize;
        var width = tmp.preferredWidth;
        if (tmpRect.GetWidth() > maxWidth)
        {
            fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
            tmp.enableAutoSizing = true;
            tmpRect.SetWidth(maxWidth);
            width = maxWidth;
        }

        if (arrowRect)
        {
            var tmpX = -arrowRect.GetWidth() / 2;
            tmpRect.anchoredPosition = new Vector2(tmpX,0);
            arrowRect.anchoredPosition = new Vector2(width / 2,0);
        }
    }

    public void SetTextCritical(string content)
    {
        if (fontSize == -1)
        {
            fontSize = tmp.fontSize;
        }
        fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
        tmp.enableAutoSizing = false;
        tmp.text = content;
        tmp.fontSize = fontSize;
        var width = tmp.preferredWidth;
        if (arrowRect)
        {
            var tmpX = arrowRect.GetWidth() / 2;
            tmpRect.anchoredPosition = new Vector2(-width / 2 + tmpX,0);
            arrowRect.anchoredPosition = new Vector2(-width / 2,0);
        }
    }
    
    public void SetTextBuff(string content,int maxWidth)
    {
        if (fontSize == -1)
        {
            fontSize = tmp.fontSize;
        }
        fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
        tmp.enableAutoSizing = false;
        tmp.text = content;
        tmp.fontSize = fontSize;
        
        var width = tmp.preferredWidth;
        if (tmpRect.GetWidth() > maxWidth)
        {
            fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
            tmp.enableAutoSizing = true;
            tmpRect.SetWidth(maxWidth);
            width = maxWidth;
        }

        if (arrowRect)
        {
            var tmpX = arrowRect.GetWidth() / 2;
            tmpRect.anchoredPosition = new Vector2(-width / 2 + tmpX,0);
            arrowRect.anchoredPosition = new Vector2(-width / 2,0);
        }
    }

}
